package com.dsg.service.apply.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dsg.entity.apply.DsgApplyBill;
import com.dsg.entity.apply.DsgApplyResult;
import com.dsg.dao.apply.DsgApplyResultMapper;
import com.dsg.entity.dto.CheckResultDto;
import com.dsg.entity.dto.ResultDetailDto;
import com.dsg.service.DsgServiceImpl;
import com.dsg.service.apply.IDsgApplyResultService;
import com.powerbridge.core.util.toolbox.CollectionUtil;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 業務申請單內審結果 服务实现类
 * </p>
 *
 * @author system
 * @since 2020-08-31
 */
@Service
public class DsgApplyResultServiceImpl extends DsgServiceImpl<DsgApplyResultMapper, DsgApplyResult> implements IDsgApplyResultService {

    @Override
    public List<ResultDetailDto> resultDetail(String appNo) {
        return baseMapper.resultDetail(appNo);
    }

    public Page<CheckResultDto> selectCheckResult(Page<CheckResultDto> page, String appNo, String entCode, Integer keepType) {
        return baseMapper.selectCheckResult(page, appNo, entCode, keepType);
    }

    @Override
    public boolean isAuditPass(DsgApplyBill bill) {
        LambdaQueryWrapper<DsgApplyResult> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DsgApplyResult::getAppNo, bill.getAppNo()).eq(DsgApplyResult::getResultType, DsgApplyResult.RESULT_TYPE_MAN)
                .eq(DsgApplyResult::getResultStatus, DsgApplyResult.RESULT_STATUS_SUCCESS);
        DsgApplyResult manAuditResult = getOne(queryWrapper);
        if (manAuditResult == null)
            return false;

        queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DsgApplyResult::getAppNo, bill.getAppNo()).eq(DsgApplyResult::getResultType, DsgApplyResult.RESULT_TYPE_CAR)
                .eq(DsgApplyResult::getResultStatus, DsgApplyResult.RESULT_STATUS_SUCCESS);
        List<DsgApplyResult> list = list(queryWrapper);
        if (CollectionUtil.isEmpty(list))
            return false;

        queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DsgApplyResult::getAppNo, bill.getAppNo()).eq(DsgApplyResult::getResultType, DsgApplyResult.RESULT_TYPE_PAY)
                .eq(DsgApplyResult::getResultStatus, DsgApplyResult.RESULT_STATUS_FAIL);
        list = list(queryWrapper);
        if (CollectionUtil.isNotEmpty(list))
            return false;

        return true;
    }
}
